// pages/ucenter/order-detail/order-detail.js
import Dialog from '../../../lib/vant-weapp/dialog/dialog';
const jeegot = require('../../../utils/jeegot.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '订单详情',
    })
    jeegot.getOrderDetail({orderId: options.id}).then(data =>{
      this.setData({
        order: data
      })
    }).catch(error =>{

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // let data = wx.getStorageSync("currOrder");
    // this.setData({
    //   order: data
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  chooseAddress: function () {
    if (this.data.order.orderStatus != 1) {
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/address/index/index?chooseMode=true&addressId=' + this.data.address.id,
    })
  },
  toCall: function () {
    wx.makePhoneCall({
      phoneNumber: '028-8888888'
    })
  },
  toPay: function (e) {
    let actualPrice = e.currentTarget.dataset.value.payAmount;
    let id = e.currentTarget.dataset.value.id;
    jeegot.payOrder({orderId: id}).then(resp =>{
      let wxPay = resp;
      wx.requestPayment({
        'timeStamp': String(wxPay.timeStamp),
        'nonceStr': wxPay.nonceStr,
        'package': wxPay.package,
        'signType': wxPay.signType,
        'paySign': wxPay.paySign,
        'success': function (res) {
          wx.redirectTo({ url: '/pages/pay-result/pay-result?status=1&actualPrice=' + actualPrice})
        },
        'fail': function (res) {
          wx.redirectTo({ url: '/pages/pay-result/pay-result?status=0&actualPrice=' + actualPrice})
        }
      })
    }).catch(error => {

    })
  },
  toProduct: function (e) {
    let id = e.currentTarget.dataset.value.id;
    wx.navigateTo({
      url: '/pages/product/product?id=' + id,
    })
  },
  cancelOrder: function(){
    Dialog.confirm({
      message: '要取消此订单？'
    }).then(() => {
      // on confirm
      this.setData({
        'order.orderStatus': 0
      })
    }).catch(() => {
      // on cancel
    });
  },
  confirmReceive: function (e) {
    Dialog.confirm({
      message: '确收到货物？'
    }).then(() => {
      // on confirm
      let index = e.currentTarget.dataset.index;
      this.setData({
        'order.orderStatus': 4
      })
    }).catch(() => {
      // on cancel
    });
  },
  toComment: function (v) {
    wx.navigateTo({
      url: '/pages/ucenter/to-comment/to-comment'
    })
  },
  toExpress: function (v) {
    wx.navigateTo({
      url: '/pages/ucenter/express/express'
    })
  },
  deleteOrder: function () {
    Dialog.confirm({
      message: '要删除此订单？'
    }).then(() => {
      // on confirm
      wx.navigateBack({
        delta: 1
      })
    }).catch(() => {
      // on cancel
    });
  },
  buyAgain: function (e) {
    let index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/product/product?id=' + this.data.order.productList[index].id
    })
  }
})